import requests
from bs4 import BeautifulSoup
import os
from pythonwork.Art.ThreadDownLoad import ThreadDownLoad


class Send:
    def send_request(self, url, types): #发送链接
        if types is None:
            types = "text"
        response = requests.get(url)
        try:
            if response.status_code == 200:
                print("请求成功:"+url)
                if types == "text":
                    response.encoding = "UTF-8"
                    return response.text
                return response.content
            else:
                print("链接失败")
        except IOError:
            print("链接错误")


    # 返回首頁解析 (set)
    def parse_set(res_html):
        print("首页解析开始")
        soup = BeautifulSoup(res_html)
        a_tag = soup.find_all(class_="a1")
        set_url = "http://www.setuw.com"
        pic_list = []
        for info in a_tag:
            href = set_url+info["href"]
            pic_list.append(href)
        print("首页解析结束")
        return pic_list

    #詳情頁解析
    def parse_detail(self, detail_url):
        print("详情页解析:"+detail_url)
        res = self.send_request(detail_url, "text")
        detail_soup = BeautifulSoup(res)
        title = detail_soup.title.string
        img = detail_soup.select(".small img")
        print(title)
        pic_list = []
        for pic in img:
            datas = pic["datas"]
            data = eval(datas)["se"] #获取大图地址
            pic_list.append(data)
        load_dict = {
                "title": title,
                "pic_list": pic_list
                }
        print("详情页解析结束")
        return load_dict

    def down_load_pic(load_dict):
        title = load_dict["title"]
        new_title = title.replace("/", "")
        print("文件开始下载:"+new_title)
        lib = "D:\set/"
        if not os.path.exists(lib):
            os.makedirs(lib)

        pic_list = load_dict["pic_list"]
        i = 0
        for pic in pic_list:
            content = Send.send_request(pic, "content")
            file_path = lib + str(i)+".jpg"
            i += 1
            file = open(file_path, "wb")
            file.write(content)
            file.close()
        print("文件下载结束")

    def start(self):
        url = "http://www.setuw.com/tag/xinggan/"
        res = self.send_request(url, "text")
        load_dicts = Send.parse_set(res)
        i = 0
        for lis in load_dicts:
            print(lis)
            print("线程创建开始"+str(i))
            load_dict = self.parse_detail(lis)
            thread = ThreadDownLoad(load_dict, i)
            i += 0
            thread.start()
            #Send.down_load_pic(load_dict)


if __name__ == "__main__":
   # url_group = ["http://www.setuw.com/tag/xinggan/", "http://www.setuw.com/tag/youhuo/"]
    Send.start(Send())
   # pool = Pool()
    #pool.map(Send.start(Send()), url_group)